home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
program
/
slix0987.zip
/
VGADM10.ZIP
/
GFX2.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-01-22
|
19KB
|
470 lines
/////////////////////////////////////////////////////////////////////////////
// //
// GFX2.CPP - VGA Trainer Program secondary module containing graphics //
// functions. Note: This module does not follow a lot of good //
// programming practices. It was built to be used with the //
// VGA tutorial series. If you are planning on using this //
// module with a different source file, some modifications may //
// be necessary. //
// //
// Author : Grant Smith (Denthor) - denthor@beastie.cs.und.ac.za //
// Translator : Christopher G. Mann - r3cgm@dax.cc.uakron.edu //
// //
// Last Modified : January 21, 1995 //
// //
/////////////////////////////////////////////////////////////////////////////
// //
// INCLUDE FILES //
// //
#include <alloc.h>
// farcalloc(), farfree()
#include <dos.h>
// geninterrupt(), FP_SEG
// //
// DEFINES //
// //
#if !defined(PI)
#define PI 3.1415927
#endif
#if !defined(VGA)
#define VGA 0xA000
#endif
// //
// TYPEDEFS //
// //
typedef unsigned char byte;
typedef unsigned int word;
// //
// FUNCTION PROTOTYPES //
// //
// SCREEN FUNCTIONS
void SetUpVirtual(byte far *&Virscr, word &Vaddr);
void ShutDown (byte far *&Virscr);
void Cls (byte Col, word Where);
void Flip (word source, word dest);
void WaitRetrace ();
// MODE SETTING FUNCTIONS
void SetMCGA ();
void SetText ();
// PALLETTE CLASS (DATA OBJECT AND RELATED FUNCTIONS)
class Pal {
public:
Pal();
void PalSet (byte Rset, byte Gset, byte Bset);
void PalGet (byte Col);
void PalPut (byte Col);
void PalInc ();
void PalDec ();
private:
byte R; // 0-63
byte G; // 0-63
byte B; // 0-63
};
// MATH-LIKE FUNCTIONS
float rad (float theta);
int sgn (int a);
template<class T>
T abso(T value) { if (value >= 0) return value; else return -value; }
// DRAWING FUNCTIONS
void Putpixel (word X, word Y, byte Col, word Where);
void PutpixelVGA (word X, word Y, byte Col);
void Line (int a, int b, int c, int d, int col, word Where);
void Hline (word X1, word X2, word Y, byte Col, word Where);
//-------------------------VIRTUAL SCREEN FUNCTIONS------------------------//
/////////////////////////////////////////////////////////////////////////////
// //
// SetUpVirtual() - This sets up the memory needed for a virtual screen. //
// //
/////////////////////////////////////////////////////////////////////////////
void SetUpVirtual(byte far *&Virscr, word &Vaddr) {
Virscr = (byte far *) farcalloc(64000,1);
Vaddr = FP_SEG(Virscr);
}
/////////////////////////////////////////////////////////////////////////////
// //
// ShutDown() - This frees the memory used by a virtual screen. //
// //
/////////////////////////////////////////////////////////////////////////////
void ShutDown(byte far *&Virscr) {
farfree(Virscr);
}
/////////////////////////////////////////////////////////////////////////////
// //
// Cls() - This clears the screen at Where to color Col. //
// //
/////////////////////////////////////////////////////////////////////////////
void Cls(byte Col, word Where) {
asm {
push es // save ES
mov cx, 32000 // this is our loop counter. we want to clear
// 64000 bytes of memory, so why do we use 32000?
// 1 word = 2 bytes, and we are moving a word at
// a time
mov es, [Where] // move address in Where to ES
xor di, di // zero out DI
mov al, [Col] // move color to AL
mov ah, al // move color to AH (Remember, will be moving
// a WORDS, so we need two copies
rep stosw // copy AX to Where and drecrement CX by 1
// until CX equals 0
pop es // restore ES
}
}
/////////////////////////////////////////////////////////////////////////////
// //
// Flip() - This copies 64000 bytes from "source" to "destination". //
// //
/////////////////////////////////////////////////////////////////////////////
void Flip(word source, word dest) {
asm {
push ds // save DS
mov ax, [dest] // copy segment of destination to AX
mov es, ax // set ES to point to destination
mov ax, [source] // copy segment of source to AX
mov ds, ax // set DS to point to source
xor si, si // zero out SI
xor di, di // zero out DI
mov cx, 32000 // set our counter to 32000
rep movsw // move source to destination by words. decrement
// CX by 1 each time until CX is 0
pop ds // restore DS
}
}
/////////////////////////////////////////////////////////////////////////////
// //
// WaitRetrace() - This waits until you are in a Verticle Retrace. //
// //
/////////////////////////////////////////////////////////////////////////////
void WaitRetrace() {
_DX = 0x03DA;
l1: asm {
in al,dx;
and al,0x08;
jnz l1;
}
l2: asm {
in al,dx;
and al,0x08;
jz l2;
}
}
//--------------------------MODE SETTING FUNCTIONS-------------------------//
/////////////////////////////////////////////////////////////////////////////
// //
// SetMCGA() - This function gets you into 320x200x256 mode. //
// //
/////////////////////////////////////////////////////////////////////////////
void SetMCGA() {
_AX = 0x0013;
geninterrupt (0x10);
}
/////////////////////////////////////////////////////////////////////////////
// //
// SetText() - This function gets you into text mode. //
// //
/////////////////////////////////////////////////////////////////////////////
void SetText() {
_AX = 0x0003;
geninterrupt (0x10);
}
//----------------------------PALLETTE FUNCTIONS---------------------------//
/////////////////////////////////////////////////////////////////////////////
// //
// Pal() - This constructor initializes all Pal variables (R, G, and B) to //
// zero. This ensures that all Pal objects start in a consistent //
/